#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;

int gNum = 100;

void test()
{
    int num = 10;
    int age = 20;
    string name("wangdao");

    //值传递
    [num, name](const string &value){
        cout << "num = " << num << endl;
        cout << "name = " << name << endl;
        cout << "value = " << value << endl;
        cout << "gNum = " << gNum << endl;
    }("hello");

    cout << endl << endl;
    [&num, name](const string &value){
        ++num;
        cout << "num = " << num << endl;
        cout << "name = " << name << endl;
        cout << "value = " << value << endl;
    }("hello");

    cout << endl << endl;
    [&num, &name](const string &value){
        ++num;
        name = "wuhan";
        cout << "num = " << num << endl;
        cout << "name = " << name << endl;
        cout << "value = " << value << endl;
    }("hello");

    cout << endl << endl;
    cout << "num = " << num << endl;
    cout << "name = " << name << endl;

    cout << endl << "[=, &]"<< endl;
    //num使用引用传递，其他变量使用值传递
    [=, &num](const string &value){
        ++num;
        cout << "num = " << num << endl;
        cout << "name = " << name << endl;
        cout << "age = " << age << endl;
        cout << "value = " << value << endl;
    }("hello");

    cout << endl << "[&, num]"<< endl;
    //num使用值传递，其他变量使用引用传递
    [&, num](const string &value){
        name = "wangdao";
        age = 30;
        cout << "num = " << num << endl;
        cout << "name = " << name << endl;
        cout << "age = " << age << endl;
        cout << "value = " << value << endl;
    }("hello");

}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

